package com.app.utils;

import cn.hutool.core.lang.Console;
import com.app.model.LotteryRecord;
import com.app.service.ILotteryRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class TaskUtils {


    @Autowired
    private ILotteryRecordService recordService;

    /**
     * 判断有效时长抽奖
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void judgmentValidDurationLottery() {
        Console.log("判断有效时长抽奖计时开始");
        QueryWrapper<LotteryRecord> lotteryRecordQueryWrapper = new QueryWrapper<>();
        lotteryRecordQueryWrapper.isNotNull("effective_time");
        lotteryRecordQueryWrapper.eq("is_hit", 1);
        lotteryRecordQueryWrapper.eq("is_send", 1);
        lotteryRecordQueryWrapper.eq("type", 1);
        // 获取当前时间
        Date now = new Date();
        // 添加条件，effective_time大于当前时间
        lotteryRecordQueryWrapper.lt("effective_time", now);
        lotteryRecordQueryWrapper.last("limit 1");
        LotteryRecord record = this.recordService.getOne(lotteryRecordQueryWrapper);
        if (record != null) {
            record.setIsSend(4);
            this.recordService.updateById(record);
            Console.log("抽奖有效计时修改成功");
        }
    }


}
